﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSPortal.Entity;

namespace BSPortal.Usercontrol.Admin.Book.Location
{
    public partial class frmLocation : System.Web.UI.Page
    {
        private MainService service = new MainService();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        void LoadCity(short id)
        {
            var data = service.GetCityById(id);
            if(data!=null)
            {
                txtId.Text = data.CityId.ToString();
                txtName.Text = data.Name;
            }
        }

        void LoadDistrict(short districtId)
        {
            var data = service.GetDistrictById(districtId);
            if(data!=null)
            {
                txtId.Text = data.CityId.ToString();
                txtName.Text = data.Name;
            }
            var listCity = service.GetAllCity();
            if(listCity!=null)
            {
                ddlCity.DataTextField = "Name";
                ddlCity.SelectedValue = "CityId";
                ddlCity.DataSource = listCity;
                if (data != null && data.CityId.HasValue) ddlCity.SelectedValue = data.CityId.Value.ToString();
            }
        }

        void LoadData()
        {
            string strWard = Request["WardId"];
            string strDistrict = Request["DistrictId"];
            string strCity = Request["CityId"];
            string strType = Request["Type"];
            if(strType!=null)
            {
                switch (strType)
                {
                    case "city":
                        trCity.Visible = false;
                        trDistrict.Visible = false;
                        if(strCity!=null)
                        {
                            var data = service.GetCityById(short.Parse(strCity));
                            if (data != null)
                            {
                                txtId.Text = data.CityId.ToString();
                                txtId.ReadOnly = true;
                                txtName.Text = data.Name;
                            }
                        }
                        break;
                    case "district":
                        trDistrict.Visible = false;
                        List<City> datalistCity = service.GetAllCity();
                        if (datalistCity == null)
                        {
                            datalistCity = new List<City>();
                        }
                        datalistCity.Insert(0, new City() { CityId = -1, Name = "Chọn Tỉnh/Tp" });
                        ddlCity.DataTextField = "Name";
                        ddlCity.DataValueField = "CityId";
                        ddlCity.DataSource = datalistCity;
                        ddlCity.DataBind();
                        if(strDistrict!=null)
                        {
                            var datadis = service.GetDistrictById(short.Parse(strDistrict));
                            txtId.Text = datadis.DistrictId.ToString();
                            txtId.ReadOnly = true;
                            txtName.Text = datadis.Name;
                            ddlCity.SelectedValue = datadis.CityId.ToString();
                        }
                        break;
                    case "ward":
                        List<City> datalistCity2 = service.GetAllCity();
                        if (datalistCity2 == null)
                        {
                            datalistCity2 = new List<City>();
                        }
                        datalistCity2.Insert(0, new City() { CityId = -1, Name = "Chọn Tỉnh/Tp" });
                        ddlCity.DataTextField = "Name";
                        ddlCity.DataValueField = "CityId";
                        ddlCity.DataSource = datalistCity2;
                        ddlCity.DataBind();
                        
                        if(strWard!=null)
                        {
                            var dataWard = service.GetWardById(short.Parse(strWard));
                            if(dataWard != null)
                            {
                                txtId.Text = dataWard.WardId.ToString();
                                txtId.ReadOnly = true;
                                txtName.Text = dataWard.Name;
                                var itemDistrict = service.GetDistrictById((int)dataWard.DistrictId);
                                ddlCity.SelectedValue = itemDistrict.CityId.ToString();
                                var listDistrict = service.GetAllDistrict((int)itemDistrict.CityId);
                                if(listDistrict!=null && listDistrict.Count>0)
                                {
                                    ddlDistrict.DataSource = listDistrict;
                                    ddlDistrict.DataValueField = "DistrictId";
                                    ddlDistrict.DataTextField = "Name";
                                    ddlDistrict.DataBind();

                                    ddlDistrict.SelectedValue = dataWard.DistrictId.ToString();
                                }
                            }
                        }
                        break;
                }
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (txtName.Text == "")
            {
                lblStatus.Text = "Tên không được để trống!";
                return;
            }
            string strWard = Request["WardId"];
            string strDistrict = Request["DistrictId"];
            string strCity = Request["CityId"];
            string strType = Request["Type"];
            int valuesRetCode = -1;
            if (strType != null)
            {
                switch (strType)
                {
                    case "city":
                        if (strCity != null)
                        {
                            valuesRetCode = service.UpdateCity(int.Parse(strCity),txtName.Text.Trim());
                            InfoUpdate(valuesRetCode);
                        }else
                        {
                            valuesRetCode = service.CreateCity(txtName.Text.Trim());
                            InfoCreate(valuesRetCode);
                        }

                        break;
                    case "district":
                        if(ddlCity.SelectedValue == "-1")
                        {
                            lblStatus.Text = "Chưa Chọn Tỉnh/Tp!";
                            return;
                        }
                        if(strDistrict!=null)
                        {
                            valuesRetCode = service.UpdateDistrict(int.Parse(strDistrict), txtName.Text,
                                                                        int.Parse(ddlCity.SelectedValue));
                            InfoUpdate(valuesRetCode);
                        }else
                        {
                            valuesRetCode = service.CreateDistrict(txtName.Text, int.Parse(ddlCity.SelectedValue));
                            InfoCreate(valuesRetCode);
                        }
                        break;
                    case "ward":
                        if (ddlCity.SelectedValue == "-1")
                        {
                            lblStatus.Text = "Chưa Chọn Tỉnh/Tp!";
                            return;
                        }
                        if (ddlDistrict.SelectedValue == "-1")
                        {
                            lblStatus.Text = "Chưa Chọn Huyện/Quận";
                            return;
                        }
                        if(strWard!=null)
                        {
                            valuesRetCode = service.UpdateWard(int.Parse(strWard), txtName.Text,
                                                               int.Parse(ddlDistrict.SelectedValue));
                            InfoUpdate(valuesRetCode);
                        }else
                        {
                            valuesRetCode = service.CreateWard(txtName.Text, int.Parse(ddlDistrict.SelectedValue));
                            InfoCreate(valuesRetCode);
                        }
                        break;
                }
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            //txtName.Text = string.Empty;
            //txtPrice.Text = string.Empty;
            //ddlParentCategory.SelectedIndex = 0;
            //ddlPriceType.SelectedIndex = 0;
            //ddlCity.SelectedIndex = 0;
        }
        public void InfoUpdate(int valuesRetCode)
        {
            if (valuesRetCode == (int)Common.RetCode.SUCESS)
            {
                lblStatus.Text = "Cập nhật thành công";
                hdModified.Value = "true";
            }
            else
            {
                lblStatus.Text = "Cập nhật không thành công";
            }
        }
        public void InfoCreate(int valuesRetCode)
        {
            if (valuesRetCode == (int)Common.RetCode.SUCESS)
            {
                lblStatus.Text = "Thêm thành công";
                hdModified.Value = "true";
            }
            else
            {
                lblStatus.Text = "Thêm không thành công";
            }
        }

        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            var listDistrict = service.GetAllDistrict(int.Parse(ddlCity.SelectedValue));
            if (listDistrict != null && listDistrict.Count > 0)
            {
                ddlDistrict.DataSource = listDistrict;
                ddlDistrict.DataValueField = "DistrictId";
                ddlDistrict.DataTextField = "Name";
                ddlDistrict.DataBind();
            }
        }
    }
}